package core

import (
	"github.com/natefinch/lumberjack"
)

const fileWriterConfKey = "file-writer"

type FileWriter struct {
	FileName   string `mapstructure:"filename"`
	MaxSize    int    `mapstructure:"max-size"`
	MaxAge     int    `mapstructure:"max-age"`
	MaxBackups int    `mapstructure:"max-backups"`
}

var defaultFileWriter *lumberjack.Logger

func (f *FileWriter) load() *lumberjack.Logger {
	return &lumberjack.Logger{
		Filename:   f.FileName,
		MaxSize:    f.MaxSize,
		MaxBackups: f.MaxBackups,
		MaxAge:     f.MaxAge,
	}
}

func GetFileWriter() *lumberjack.Logger {
	if defaultFileWriter != nil {
		return defaultFileWriter
	}

	var (
		conf FileWriter
		err  error
	)

	//获取配置
	if conf, err = Conf[FileWriter](fileWriterConfKey); err != nil {
		panic(err)
	}

	defaultFileWriter = conf.load()
	return defaultFileWriter
}
